package cn.xuchunh.campusbbs.ui.fragments.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.utils.FileUtil;

/**
 * Created by XuChunH on 2015/10/31.
 */
public class ClearCacheDialog extends DialogFragment {

    private OnClearCacheListener listener;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
                .setMessage("确定清空缓存？")
                .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        boolean result = FileUtil.deleteFile(getActivity().getCacheDir());
                        if (listener != null) {
                            listener.cacheCleared(result);
                        }
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create();
    }

    public void setOnClearCacheListener(OnClearCacheListener listener) {
        this.listener = listener;
    }

    public interface OnClearCacheListener {
        void cacheCleared(boolean result);
    }

}
